/* 
 * Copyright (C) 2006, Dung-Bang Tsai
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * ( If you wnat to use this library for commercial use, 
 *	  feel free to contact me, just cost some money, I could sell
 *		you the code without GPL license, so you could use this code
 *		for your product without public your source code.  				)
 *
 * Authors:
 *	Tsai, Dung-Bang <dbtsai@gmail.com>
 *			
*			2006/03/05		at NCKU physics
 */
string NumbertoHex(unsigned short int input)
{   string result="    ";//unsigned char result[5];
	unsigned char table[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	result[0]=0;	result[1]=0;	result[2]=0;		result[3]=0;

	unsigned int aa,bb,cc,dd;
	
	aa = input >> 12;
	bb = (input& 0x0F00)  >> 8;
	cc = (input & 0x00F0) >> 4;
	dd = (input & 0X000F) ;

	result[0]=table[aa];
	result[1]=table[bb];
	result[2]=table[cc];
	result[3]=table[dd];
	return result;
}
string Number32toHex(unsigned int input)
{   string result="        ";//unsigned char result[5];
	unsigned char table[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	result[0]=0;	result[1]=0;	result[2]=0;		result[3]=0; 
	result[4]=0;	result[5]=0;	result[6]=0;		result[7]=0; 

	unsigned int aa,bb,cc,dd,ee,ff,gg,hh;
	
	aa = input >> 28;
	bb = (input & 0x0F000000)  >> 24;
	cc = (input & 0x00F00000) >> 20;
	dd = (input & 0X000F0000) >> 16;
	ee = (input & 0x0000F000) >> 12;
	ff = (input & 0x00000F00) >> 8;
	gg = (input & 0X000000F0) >> 4;
	hh = (input & 0X0000000F) ;
	
	result[0]=table[aa];
	result[1]=table[bb];
	result[2]=table[cc];
	result[3]=table[dd];
	result[4]=table[ee];
	result[5]=table[ff];
	result[6]=table[gg];
	result[7]=table[hh];
	return result;
}
unsigned int aHex(char input)
{
	unsigned int table[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; //48~57, 65~70, 
	if('0'<=input && input <= '9')
		return table[input-'0'];
	if('A'<=input && input <= 'F')
		return table[input-55];
	if('a'<=input && input <= 'f')
		return table[input-87];	
	else
		return 0;
}
unsigned short int HexToNumber(unsigned char* input)
{
	unsigned int table[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; //48~57, 65~70, 
	return (aHex(input[0])*16*16*16) + (aHex(input[1])*16*16) + (aHex(input[2])*16) + aHex(input[3]);
}

unsigned int HexToNumber32(unsigned char* input)
{
	int a = 16;int b=a*16; int c=b*16; int d=c*16; int e=d*16; int f=e*16;int g=f*16;
	unsigned int table[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; //48~57, 65~70, 
	return ( aHex(input[0])*g + aHex(input[1])*f + 
			   aHex(input[2])*e + aHex(input[3])*d + 
				aHex(input[4])*c + aHex(input[5])*b + 
				aHex(input[6])*a + aHex(input[7])	);
}
